package DataStorage;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import HibernateSQLite.java.model.Critere;
import HibernateSQLite.java.model.CritereText;

public class CritereDAOHibernate extends
		GenericDAOHibernate<Critere, Long, ICritereDAO> implements ICritereDAO {

	@SuppressWarnings("unchecked")
	public boolean isExist(String critere_name) {
		try {
			List<Critere> list = getSession().createCriteria(Critere.class)
					.add(Restrictions.eq("critere_name", critere_name)).list();

			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		return false;
	}

	@SuppressWarnings("unchecked")
	public long getCritere_id(String critere_name) {
		try {
			List<Critere> list = getSession().createCriteria(Critere.class)
					.add(Restrictions.eq("critere_name", critere_name)).list();

			for (Critere c : list) {
				return c.getCritere_id();
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return (Long) null;
	}

	@SuppressWarnings("unchecked")
	public Critere getCritere(long critere_id) {
		try {
			List<Critere> list = getSession().createCriteria(Critere.class)
					.add(Restrictions.eq("critere_id", critere_id)).list();

			for (Critere c : list) {
				return c;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public Critere getCritere(String critere_name) {
		try {
			List<Critere> list = getSession().createCriteria(Critere.class)
					.add(Restrictions.eq("critere_name", critere_name)).list();

			for (Critere c : list) {
				return c;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public String getCritereName(long critere_id) {
		try {
			List<Critere> list = getSession().createCriteria(Critere.class)
					.add(Restrictions.eq("critere_id", critere_id)).list();

			for (Critere c : list) {
				return c.getCritere_name();
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	/*
	 * public void saveCritere(String critere_name){
	 * 
	 * Critere critere = null; if(!this.isExist(critere_name)){ critere = new
	 * Critere(); critere.setCritere_name(critere_name);
	 * getSession().save(critere); }
	 * 
	 * //this.commit();
	 * 
	 * }
	 */
	public void saveCritere(String critere_name) {

		Critere critere = null;
		Session session = null;
		Transaction ts = null;
		try {
			session = this.getSession();
			ts = session.beginTransaction();
			if (!this.isExist(critere_name)) {
				critere = new Critere();
				critere.setCritere_name(critere_name);
				session.save(critere);
			}
		} catch (HibernateException e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		// this.commit();

	}

	public void deleteCritere(String critere_name) {

		Critere critere = null;
		Session session = null;
		Transaction ts = null;
		try {
			session = this.getSession();
			ts = session.beginTransaction();

			if (this.isExist(critere_name)) {
				critere = this.getCritere(critere_name);
				long critere_id = critere.getCritere_id();
				CritereTextDAOHibernate critereTextDAO = new CritereTextDAOHibernate();
				List<CritereText> listCritereText = critereTextDAO
						.getListCritereText(critere_id);
				for (CritereText critereText : listCritereText) {
					critereText.getCritere().getCritereText()
							.remove(critereText);
					critereText.setCritere(null);
					session.delete(critereText);
					// critereTextDAO.deleteCritereText(critereText);
				}

				session.delete(critere);

			} else {
				System.out.println("Does not exist this critere !");
			}
			ts.commit();

		} catch (HibernateException e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

	}

	public static void main(String[] args) {
		CritereDAOHibernate c = new CritereDAOHibernate();
		// long id = c.getCritere_id("X91_Essieu 4RD");
		// c.delete(critere);
		// c.deleteCritere("X91_Essieu 2RD");
		// c.deleteCritere("X95_Types 1,2,A,B,C");
		//c.deleteCritere("X61_Essieu");
		// System.out.println(c.getEntity(id));
		long critere_id = 18;
		System.out.println( c.getCritereName(critere_id));
	}

}
